import http from '@/api';
import { ADMIN_MODULE } from '@/api/helper/prefix';
import type { IUploadResult, UploadFile, UploadResult } from '@/api/types/system/upload';
import type { AxiosRequestConfig } from 'axios';

/**
 * 上传文件
 * @param params
 * @returns {*}
 */
export const uploadFile = (params: UploadFile, config?: AxiosRequestConfig<any> | undefined) => {
  return http.upload<UploadResult>(ADMIN_MODULE + `/sys-file/upload`, params, config);
};

/**
 * 上传模板文件
 * @param params
 * @param config
 * @returns {*}
 */
export const uploadTmpFile = (params: UploadFile, config?: AxiosRequestConfig<any> | undefined) => {
  return http.upload<IUploadResult>(ADMIN_MODULE + `/sys-temp-file/upload`, params, config);
};


/**
 * 上传文件
 * @param params
 * @returns {*}
 */
export const uploadFile2 = (params: {uploadSelf : File | any}, config?: AxiosRequestConfig<any> | undefined) => {
  return http.upload<UploadFile>(ADMIN_MODULE + `/sys-file/uploadSelf`, params, config);
};


/**
 * 批量上传文件
 * @param params
 * @returns {*}
 */
export const batchUploadSelf = (params: { uploadSelfs: File[] }, config?: AxiosRequestConfig<any> | undefined) => {
  const formData = new FormData();

  // 将图片文件添加到 FormData 中
  params.uploadSelfs.forEach(file => {
    formData.append('uploadSelfs', file);
  });

  return http.upload<UploadFile>(ADMIN_MODULE + `/sys-file/batchUploadSelf`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data',  // 设置上传文件的 Content-Type
      ...config?.headers,  // 保留原有的 config 配置
    },
    ...config,  // 其他配置
  });
};
